#region Usings

using System;
using Newtonsoft.Json;
using Karma.Framework.Core.Metadata.Mapping;

#endregion

namespace Karma.Framework.Core.Metadata.Converter
{
    public class QueryConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value)
        {
            var consulta = value as Query;
            if (consulta == null)
            {
                throw new ArgumentException("Value debe ser un Query");
            }
            writer.WriteStartObject();
            writer.WritePropertyName("Id");
            writer.WriteValue(consulta.Id);
            writer.WritePropertyName("Nombre");
            writer.WriteValue(consulta.Name);
            writer.WritePropertyName("LinkQuery");
            writer.WriteValue(consulta.LinkQuery);
            writer.WriteEndObject();
        }

        public override object ReadJson(JsonReader reader, Type objectType)
        {
            throw new InvalidOperationException();
        }

        public override bool CanConvert(Type objectType)
        {
            return objectType.Equals(typeof(Query));
        }
    }
}